package com.fairytask.ui
{
	import com.fairytask.data.BaseData;
	import com.fairytask.data.Record;
	import com.fairytask.data.RecordObject;
	import com.fairytask.engine.GameStage;
	import com.fairytask.engine.define.CoolDefine;
	import com.fairytask.engine.define.RecordDefine;
	import com.fairytask.managers.ResManager;
	import com.fairytask.ui.base.FrameUI;
	import com.fairytask.ui.item.CoolDownItem;
	import com.fairytask.ui.main.ChatUI;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;

	/**
	 * 冷却系统
	 * @author yangj
	 **/
	
	public class CoolDownTimeUI extends FrameUI
	{
		private static var _self:CoolDownTimeUI;
		
		private var bg		: Shape;//灰色背景框
		private var hover 	: MovieClip;//鼠标移上效果
		
		private var itemsArr 	: Array = [];
		private var baseObj 	: BaseData;
		
		private var  static_y	: int;//初始化UI y坐标常量
		private var  static_x	: int;//初始化UI x坐标常量
		private var moveUpVal 	: int;//自适应向上移动偏移量
		private var _sp		:Sprite	=	new Sprite();
		public static function getInstance():CoolDownTimeUI
		{
			if(_self == null)
			{
				_self = new CoolDownTimeUI();
			}
			return _self;
		}
		
		public function CoolDownTimeUI(popUI:Boolean=false)
		{
			super(popUI);
			initBind();
		}
		
		override public function initGUI():void
		{
			super.initGUI();
			static_x = this.x;
			static_y = this.y;
			initX = static_x;
			initY = static_y;
			
			bg = new Shape();
			bg.graphics.beginFill(0x000000,0.7);
			bg.graphics.drawRoundRect(0,0,204,120,4,4);
			bg.graphics.endFill();
			windowBG.addChild(bg);
			windowBG.addChild(_sp);
			bg.visible = false;
			hover = ResManager.newResClass("bgFrame_2") as MovieClip;
			hover.width = 194;
			hover.x = 7;
			
			baseObj = new BaseData();
			//autoMove();
		}
		
		private function initBind():void
		{
			bindManager.BindRecord(model.scene,RecordDefine.CLIENTCOOLREC,onAddObj,onRemoveObj);
		}
		
		/**
		 * 添加冷却项
		 * @param key
		 * @param val
		 * 
		 */
		private function onAddObj(key:String, val:RecordObject):void
		{
			bg.visible = true;
			if(baseObj.CheckProperties(key))
			{
				return;
			}
			baseObj.HandleProperties(key,val);
			//Column_Declare_Int(Type, 0)			//冷却类型	
			//Column_Declare_Int(Time, 1)			//冷却时间
			//Column_Declare_Int(Start, 2)			//冷却开始时间
			var tpItem 	:CoolDownItem 	=	new CoolDownItem(val.GetProperties("0"));
			
			//tpItem.totalCount	=	val.GetProperties("4");
			itemsArr.push(tpItem);
			//绑定冷却时间
			bindManager.BindRecordProp(val,"1",onTimeChange,[tpItem,key]);
			bindManager.BindRecordProp(val,"2",onTimeChange,[tpItem,key]);
			//绑定冷却状态
			bindManager.BindRecordProp(val,"3",onStatus,[tpItem]);
			tpItem.x = 8;
			tpItem.y = 18*(itemsArr.length - 1) + 1;
//			bg.height = tpItem.y + 27;
			_sp.addChild(tpItem);
			tpItem.addEventListener(MouseEvent.ROLL_OVER,onOver);
			tpItem.addEventListener(MouseEvent.ROLL_OUT,onOut);
			tpItem.addEventListener(MouseEvent.CLICK,onClick);
			//autoMove();

		}
		
		
		
		/**
		 * 行数多了，位置自动协调向上移动(UI初始化设计正好显示3条记录到聊天窗口顶部,未来拓展多条，自适应向上移动)
		 * 
		 */		
		private function autoMove():void
		{
			if((GameStage.mainLayer.y + static_y + this.height + GameStage.mainLayer.y + this.height + ChatUI.getInstance().height)>GameStage.stageHeight)
			{
				moveUpVal = GameStage.mainLayer.y + static_y + this.height + GameStage.mainLayer.y + ChatUI.getInstance().height - GameStage.stageHeight;
				initY =  static_y - moveUpVal;
				stageReSize();
			}
		}
		
		private function onOver(evt:MouseEvent):void
		{
			hover.y = evt.currentTarget.y + 3;
			windowBG.addChild(hover);
		}
		
		private function onOut(evt:MouseEvent):void
		{
			if(windowBG.contains(hover))
			{
				windowBG.removeChild(hover);
			}
		}
		
		private function onClick(evt:MouseEvent):void
		{
			//点击的不是快速冷却按钮，弹出相应界面
			if(!(evt.currentTarget as CoolDownItem).endCoolBtn.contains(evt.target as DisplayObject))
			{
				switch((evt.currentTarget as CoolDownItem).type)
				{
					case CoolDefine.COOL_DOWN_UPGRADE_PET:
						PetsUI.getInstance().showPos(0);
						break;
					case CoolDefine.COOL_DOWN_EQUIP:
						MainUI.getInstance().showPos(2);
						break;
					case CoolDefine.COOL_DOWN_EXTRACT:
						//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_OPEN_HUNTER_PET);
						break;
					case CoolDefine.COOL_DOWN_HUNTER_LIFE:
						MainSkillUI.getInstance().showPos(1);
						break;
					case CoolDefine.COOL_DOWN_ADVANCE_SKILL:
						MainSkillUI.getInstance().showPos(0);
						break;
					case CoolDefine.COOL_DOWN_CHALLENGE:
						ArenaUI.getInstance().show();
						break;
				}
			}
		}
		
		/**
		 * 冷却状态变化 要变色就行了 
		 * @param key
		 * @param value
		 * @param pList
		 * 
		 */
		private function onStatus(key:String,value:int,pList:Array):void
		{
			(pList[0] as CoolDownItem).colorWithStatus(value);
		}
		/**
		 * 时间变化 
		 * @param key
		 * @param value
		 * @param pList
		 * 
		 */
		private function onTimeChange(key:String,value:int,pList:Array):void
		{
			var line:String			=	pList[1];
			var	coolRecord:Record	=	model.scene.GetRecordEx(RecordDefine.CLIENTCOOLREC);
			var val:RecordObject	=	coolRecord.GetProperties(line);
			var startTime	:int	=	val.GetProperties("1");
			var time		:int	=	val.GetProperties("2");
			var item	:CoolDownItem	=	pList[0] as CoolDownItem;
			if(time == 0)
			{
				item.waitState();
			}else
			{
				item.coolState(time,startTime);
			}
		}
		
		
//		override protected function BeforeShow():void
//		{
//			stageReSize();
//		}
		
		/**
		 * 删除冷却项 
		 * @param key
		 * @param val
		 * 
		 */
		private function onRemoveObj(key:String, val:RecordObject):void
		{
			var type:int	=	val.GetProperties(0);
			for(var i:int=0;i<itemsArr.length;i++)
			{
				var tpItem:CoolDownItem	=	itemsArr[i];
				if(tpItem.coolType==type)
				{
					var num:int	=	itemsArr.indexOf(tpItem);
					itemsArr.splice(num,1);
					break;
				}
			}
			refresh();
			
			
		}
		
		//重写显示
		private function refresh():void
		{
			while(_sp.numChildren>0)
			{
				_sp.removeChildAt(0);
			}
			if(itemsArr.length==0)
			{
				return;
			}
			
			for(var i:int=0;i<itemsArr.length;i++)
			{
				var tpItem:CoolDownItem	=	itemsArr[i];
				tpItem.x = 8;
				tpItem.y = 18*i + 1;
				//bg.height = tpItem.y + 27;
				_sp.addChild(tpItem);
				tpItem.addEventListener(MouseEvent.ROLL_OVER,onOver);
				tpItem.addEventListener(MouseEvent.ROLL_OUT,onOut);
				tpItem.addEventListener(MouseEvent.CLICK,onClick);
				//autoMove();
			}
			
			
		}
		
		public function coolTime(val:int):int
		{
			for each(var tmpItem:CoolDownItem in itemsArr)
			{
				if(tmpItem.coolType == val)
				{
					return tmpItem.coolTime;
				}
			}
			return 0;
		}
		
		public function timeTxt(val:int):TextField
		{
			for each(var tmpItem:CoolDownItem in itemsArr)
			{
				if(tmpItem.coolType == val)
				{
					return tmpItem.timeShowTxt;
				}
			}
			return null;
		}
		
		
		
		/**
		 * 重写显示 
		 * @param space
		 * @param onshow
		 * @param argc
		 * 
		 */		
		override public function show(space:String=BACKLAYER, onshow:Function=null):void
		{
			super.show(space,onshow);
		}
		
		
		/**
		 * 场景变化改变位置 
		 * 
		 */
		override protected function stageReSize(evt:Event=null):void
		{
			this.x	=	-GameStage.mainLayer.x +initX;
			this.y	=	GameStage.mainLayer.y + initY;
		}
	}
}